package com.example.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import com.example.base.BaseController;
import com.example.entity.*;
import com.example.service.*;
import com.example.util.BeanUtils;
import com.example.vo.Result;
import com.example.vo.SpuItemVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lsc
 * @date 2024/5/30
 */
@RestController
@Slf4j
@RequestMapping("/admin/spu")
public class ExamSpuController extends BaseController<SpuService,Spu> {

    @Resource
    private SpuAttrValueService spuAttrValueService;

    @Resource
    private SkuService skuService;

    @Resource
    private SkuAttrValueService skuAttrValueService;

    @Resource
    private GoodsCategoryService goodsCategoryService;

    @PostMapping("/takeOff")
    public Result<Boolean> takeOff(@RequestParam("spuId") Long spuId){

        int re = baseService.updateStatus(0,spuId);

        return Result.ok(re>0);
    }

    @PostMapping("/putOn")
    public Result<Boolean> putOn(@RequestParam("spuId") Long spuId){

        int re = baseService.updateStatus(1,spuId);

        return Result.ok(re>0);
    }

    @GetMapping("/getSpuList")
    public Result<List<SpuVo>> getSpuList(){
        List<Spu> spu = baseService.getExamSpuList();

        List<SpuVo> spuVo = BeanUtils.copyList(spu,SpuVo.class);

        for (SpuVo vo : spuVo) {
            Integer spuId = vo.getId().intValue();

            List<SpuAttrValue> spuAttrValueList = spuAttrValueService.getExamSpuAttrsBySpuId(spuId);

            vo.setBaseAttrs(spuAttrValueList);

            List<SkuVo> skus = skuService.getExamSkusBySpuId(spuId);

            List<Long> skuIds = new ArrayList<>();

            for (SkuVo skuVo : skus) {
                Long skuId = skuVo.getId();

                skuIds.add(skuId);

                skuVo.setSaleAttrs(skuAttrValueService.getExamSkuAttrsBySkuId(skuId));

                skuVo.setImages(skuService.getExamSkuImagesById(skuId));
            }

            vo.setSkus(skus);

            vo.setAllSaleAttrs(skuService.getExamAllSkuSaleAttrBySkuIds(skuIds));

            vo.setSpuImages(skuService.getExamAllSkusImagesById(skuIds));

            vo.setStoreName(baseService.getSpuStoreNameById(vo.getStoreId()));

            vo.setCategoryName(baseService.getSpuCategoryNameById(vo.getCategoryId()));
        }


        return Result.ok(spuVo);
    }

    @PostMapping("/accept")
    @Transactional
    public Result<Boolean> accept(@RequestParam("spuId") Long spuId){

        int temp = baseService.updatePublishStatus(1,spuId);

        int integer = 0;

        if(temp == 1){
            baseService.addExamSpuToSpu(spuId);

            baseService.addExamSkuToSku(spuId);

            baseService.addExamSpuDescToSpuDesc(spuId);

            baseService.addExamSpuAttrToSpuAttr(spuId);

            List<Long> skuIds = baseService.getExamSkuIdsBySpuId(spuId);

            baseService.addExamSkuAttrToSkuAttr(skuIds);

            baseService.addExamSkuImagesToSkuImages(skuIds);

            integer = 1;
        }else {
            Result.ok(false);
        }

        return Result.ok(integer>0);
    }

    @PostMapping("/refuse")
    public Result<Boolean> refuse(@RequestParam("spuId") Long spuId){

        int temp = baseService.updatePublishStatus(-1,spuId);

        return Result.ok(temp>0);
    }

    @GetMapping("/category/getCategoryList")
    public Result<List<GoodsCategory>> getGoodsCategoryList(){
        List<GoodsCategory> categoryList = goodsCategoryService.getGoodsCategoryList();
        return Result.ok(categoryList);
    }


    @PostMapping("/category/add")
    public Result<Boolean> addChildrenCategory(@RequestBody GoodsCategory child){
        int re = goodsCategoryService.addChildrenCategory(child);
        return Result.ok(re>0);
    }

    @PostMapping("/category/ban")
    public Result<Boolean> ban(@RequestParam("categoryId") Long categoryId){
        int re = goodsCategoryService.updateStatus(false,categoryId);
        return Result.ok(re>0);
    }

    @PostMapping("/category/open")
    public Result<Boolean> open(@RequestParam("categoryId") Long categoryId){
        int re = goodsCategoryService.updateStatus(true,categoryId);
        return Result.ok(re>0);
    }

    @PostMapping("/category/edit")
    public Result<Boolean> edit(@RequestBody GoodsCategory goodsCategory){
        int re = goodsCategoryService.editCategory(goodsCategory);
        return Result.ok(re>0);
    }

}
